package com.arges.web.serviceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.arges.web.common.DateUtil;
import com.arges.web.common.SysEnum.BloodType;
import com.arges.web.common.SysEnum.ExtractTime;
import com.arges.web.dao.BloodDao;
import com.arges.web.dao.GenericDao;
import com.arges.web.domain.bean.BloodBean;
import com.arges.web.domain.pojo.Blood;
import com.arges.web.domain.pojo.Pat;
import com.arges.web.page.PageInfo;
import com.arges.web.page.WebUtils;
import com.arges.web.service.BloodManager;
import com.arges.web.service.PatManager;

/**
 * 患者诊断治疗业务层
 *
 * @author zxj
 *
 */
@Service
public class BloodManagerImpl extends GenericManageImpl<Blood, BloodBean> implements BloodManager
{
	private Logger LOGGER = LoggerFactory.getLogger(BloodManagerImpl.class);

	@Autowired
	private BloodDao bloodDao;

	@Autowired
	private PatManager patManager;

	@Override
	public GenericDao<Blood, Long> getGenericDao()
	{
		return bloodDao;
	}

	@Override
	public BloodBean getBloodByMrn(String mrn)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageInfo<BloodBean> getPageBlood(PageInfo<Blood> pageInfo, String mrn, String startTime, String endTime)
	{
		PageInfo<BloodBean> beanPageList = null;
		try
		{
			Criterion condiition = null;
			if (StringUtils.isNotBlank(mrn.trim()))
			{
				condiition = Restrictions.like("mrn", mrn, MatchMode.ANYWHERE);
			}
			if (StringUtils.isNotBlank(startTime) && (StringUtils.isNotBlank(endTime)))
			{
				Date startDate = DateUtil.convertString2Date(startTime, DateUtil.DEFAULT_DATE_TIME);
				Date endDate = DateUtil.convertString2Date(endTime, DateUtil.DEFAULT_DATE_TIME);
				Criterion timeCriterion = null;
				timeCriterion = Restrictions.between("g_date", startDate, endDate);
				if (null == condiition)
				{
					condiition = timeCriterion;
				}
				else
				{
					condiition = Restrictions.and(condiition, timeCriterion);
				}
			}
			else if (StringUtils.isNotBlank(startTime))
			{
				Date startDate = DateUtil.convertString2Date(startTime, DateUtil.DEFAULT_DATE_TIME);
				Criterion timeCriterion = null;
				timeCriterion = Restrictions.ge("g_date", startDate);
				if (null == condiition)
				{
					condiition = timeCriterion;
				}
				else
				{
					condiition = Restrictions.and(condiition, timeCriterion);
				}
			}
			else if (StringUtils.isNotBlank(endTime))
			{
				Date endDate = DateUtil.convertString2Date(endTime, DateUtil.DEFAULT_DATE_TIME);
				Criterion timeCriterion = null;
				timeCriterion = Restrictions.le("g_date", endDate);
				if (null == condiition)
				{
					condiition = timeCriterion;
				}
				else
				{
					condiition = Restrictions.and(condiition, timeCriterion);
				}
			}
			PageInfo<Blood> pojoPageList = bloodDao.findAll(pageInfo, Blood.class, condiition);

			beanPageList = WebUtils.pojoToBeanPage(pojoPageList, BloodBean.class);

			List<Pat> patList = patManager.findAll();
			ExtractTime[] extractTimeList = ExtractTime.values();
			Map<Integer, String> extractTimeMap = new HashMap<Integer, String>();
			for (ExtractTime extractTime : extractTimeList)
			{
				extractTimeMap.put(extractTime.getId(), extractTime.name());
			}
			BloodType[] bloodTypeList = BloodType.values();
			Map<Integer, String> bloodTypeMap = new HashMap<Integer, String>();
			for (BloodType bloodType : bloodTypeList)
			{
				bloodTypeMap.put(bloodType.getId(), bloodType.name());
			}
			for (BloodBean bloodBean : beanPageList.getResult())
			{
				for (Pat pat : patList)
				{
					if (pat.getMrn().equals(bloodBean.getMrn()))
					{
						bloodBean.setP_name(pat.getName());
					}
				}
				bloodBean.setType(extractTimeMap.get(Integer.valueOf(bloodBean.getType())));
				bloodBean.setG_type(bloodTypeMap.get(Integer.valueOf(bloodBean.getG_type())));
			}

		}
		catch (Exception e)
		{
			return null;
		}
		return beanPageList;
	}

	@Override
	public BloodBean findBloodById(long parseLong)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deleteByIds(String ids)
	{
		// TODO Auto-generated method stub

	}

}
